package com.bykj.taketurns.fragment;

import android.animation.LayoutTransition;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewConfigurationCompat;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bykj.taketurns.R;
import com.bykj.taketurns.activity.PersonalActivity;
import com.bykj.taketurns.activity.ReadWordActivity;
import com.bykj.taketurns.activity.WriteScreenActivity;
import com.bykj.taketurns.widget.ReadNestedScroll;
import com.dou361.ijkplayer.utils.KeybordS;
import com.dou361.ijkplayer.widget.SlideBottomEdit;
import com.dou361.ijkplayer.widget.SlideBottomReward;
import com.dou361.ijkplayer.widget.SlideBottomShare;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/8/16.
 */

public class ReadViewFragment extends BaseFragment implements View.OnTouchListener{
    private View mRootView;
    private ReadNestedScroll read_frag_scroll;
    private TextView read_frag_tv_content;
    private ImageView mAppVideoFinish;
    private TextView mTopbarTvTitle;
    private RelativeLayout mVideoAttentionRela;
    private ImageView mVideoAddImg;
    private TextView mVideoTvAdd;
    private ImageView mVideoHeadImg;
    private boolean isAttentionStatus;
    private RelativeLayout mBottombarRelaReport;
    private ImageView mBottombarImgReport;
    private TextView mBottombarTvReport;
    private RelativeLayout mBottombarRelaLike;
    private ImageView mBottombarImgLike;
    private TextView mBottombarTvLike;
    private RelativeLayout mBottombarRelaComment;
    private ImageView mBottombarImgComment;
    private TextView mBottombarTvComment;
    private RelativeLayout mBottombarRelaTransmit;
    private ImageView mBottombarImgTransmit;
    private TextView mBottombarTvTransmit;
    private RelativeLayout mBottombarRelaReward;
    private ImageView mBottombarImgReward;
    private TextView mBottombarTvReward;
    private TextView simpleTvCancle;
    private TextView simpleTvReport;
    private ListView simpleList;
    private int index ;
    private HashMap<Integer,TextView> map=new HashMap<>();
    private boolean isLike;
    private boolean isPopUp;
    private RelativeLayout bottomView;
    private EditText bottomBarEt;
    private  final Timer timer = new Timer();
    private EditText shapeEt;
    private ImageView bottomBarCancle;
    private SlideBottomEdit slide;
    private float mInitialMotionX;
    private float mInitialMotionY;
    private int mTouchSlop;


    @Override
    protected View getRootView() {
        return View.inflate((ReadWordActivity)activity, R.layout.fragment_read,null);
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
       this.mRootView=view;
        ViewConfiguration configuration = ViewConfiguration.get(activity);
        // 获取TouchSlop值
        mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
        read_frag_scroll = view.findViewById(R.id.read_frag_scroll);
        read_frag_tv_content = view.findViewById(R.id.read_frag_tv_content);
        mAppVideoFinish = view.findViewById(R.id.app_video_finish);
        mTopbarTvTitle = view.findViewById(R.id.topbar_tv_title);
        mVideoAttentionRela = view.findViewById(R.id.video_attention_rela);
        mVideoAddImg = view.findViewById(R.id.video_add_img);
        mVideoTvAdd = view.findViewById(R.id.video_tv_add);
        mVideoHeadImg = view.findViewById(R.id.video_head_img);
        mBottombarRelaReport = view.findViewById(R.id.bottombar_rela_report);
        mBottombarImgReport = view.findViewById(R.id.bottombar_img_report);
        mBottombarTvReport = view.findViewById(R.id.bottombar_tv_report);
        mBottombarRelaLike = view.findViewById(R.id.bottombar_rela_like);
        mBottombarImgLike = view.findViewById(R.id.bottombar_img_like);
        mBottombarTvLike = view.findViewById(R.id.bottombar_tv_like);
        mBottombarRelaComment = view.findViewById(R.id.bottombar_rela_comment);
        mBottombarImgComment = view.findViewById(R.id.bottombar_img_comment);
        mBottombarTvComment = view.findViewById(R.id.bottombar_tv_comment);
        mBottombarRelaTransmit = view.findViewById(R.id.bottombar_rela_transmit);
        mBottombarImgTransmit = view.findViewById(R.id.bottombar_img_transmit);
        mBottombarTvTransmit = view.findViewById(R.id.bottombar_tv_transmit);
        mBottombarRelaReward = view.findViewById(R.id.bottombar_rela_reward);
        mBottombarImgReward = view.findViewById(R.id.bottombar_img_reward);
        mBottombarTvReward = view.findViewById(R.id.bottombar_tv_reward);
        bottomView = view.findViewById(R.id.popup_rela);
        bottomBarEt = view.findViewById(R.id.popup_comment_et);
        bottomBarCancle = view.findViewById(R.id.popup_cancle_img);
        //点击监听
        mAppVideoFinish.setOnClickListener(this);
        mVideoAttentionRela.setOnClickListener(this);
        mVideoAddImg.setOnClickListener(this);
        mVideoTvAdd.setOnClickListener(this);
        mVideoHeadImg.setOnClickListener(this);
        mBottombarRelaReport.setOnClickListener(this);
        mBottombarImgReport.setOnClickListener(this);
        mBottombarTvReport.setOnClickListener(this);
        mBottombarRelaLike.setOnClickListener(this);
        mBottombarImgLike.setOnClickListener(this);
        mBottombarTvLike.setOnClickListener(this);
        mBottombarRelaComment.setOnClickListener(this);
        mBottombarImgComment.setOnClickListener(this);
        mBottombarTvComment.setOnClickListener(this);
        mBottombarRelaTransmit.setOnClickListener(this);
        mBottombarImgTransmit.setOnClickListener(this);
        mBottombarTvTransmit.setOnClickListener(this);
        mBottombarRelaReward.setOnClickListener(this);
        mBottombarImgReward.setOnClickListener(this);
        mBottombarTvReward.setOnClickListener(this);
        bottomBarEt.setOnClickListener(this);
        bottomBarCancle.setOnClickListener(this);
        bottomBarEt.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {

                if(b){
                    timer.schedule(new TimerTask() {
                        public void run() {
                            //调用键盘
                            KeybordS.openKeybord(shapeEt,activity);

                        }
                    }, 200);
                    if(slide!=null){
                        slide.showPopupWindow();
                        Log.d("comment1","show");
                    }else {
                        selectComment();
                    }
                    slide.judgeKeybord(activity);

                }

            }
        });
        bottomBarEt.setInputType(InputType.TYPE_NULL);
        mRootView.setOnTouchListener(this);

    }

    @Override
    protected void postData() {
        read_frag_scroll.smoothScrollBy(0,0);
        read_frag_tv_content.setFocusable(false);
        read_frag_tv_content.postInvalidate();

    }

    @Override
    protected void initData() {

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.d("zzz","onDestroyView");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.app_video_finish:
                //返回主页面
                activity.onBackPressed();
                activity.finish();
                break;
            case R.id.video_attention_rela:
            case R.id.video_add_img:
            case R.id.video_tv_add:
            case R.id.video_head_img:
                //关注动画
                showMenu();
                break;
            case R.id.bottombar_rela_report:
            case R.id.bottombar_img_report:
            case R.id.bottombar_tv_report:
                //举报按钮
                //点击举报按钮时
                final AlertDialog alertDialog=new AlertDialog.Builder(activity).create();
                View vie= View.inflate(activity,R.layout.simple_alertdailog,null);
                //初始化按钮
                init(vie);
                alertDialog.show();
                alertDialog.getWindow().setContentView(vie);
                simpleTvCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
                simpleTvReport.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //提交举报内容

                    }
                });
                break;
            case R.id.bottombar_rela_like:
            case R.id.bottombar_img_like:
            case R.id.bottombar_tv_like:
                selectLikeButton();
                break;
            case R.id.bottombar_rela_comment:
            case R.id.bottombar_img_comment:
            case R.id.bottombar_tv_comment:
                //下方的view弹出
                setAnim();
                break;
            case R.id.bottombar_rela_transmit:
            case R.id.bottombar_img_transmit:
            case R.id.bottombar_tv_transmit:
                //点击转发功能
                selectTransmit();
                break;
            case R.id.bottombar_rela_reward:
            case R.id.bottombar_img_reward:
            case R.id.bottombar_tv_reward:
                /**
                 * 点击打赏按钮
                 */
                selectReward();
                break;
            case R.id.popup_cancle_img:
                isPopUp=true;
                setAnim();
                break;
            case R.id.popup_comment_et:
                timer.schedule(new TimerTask() {
                    public void run() {
                        //调用键盘
                        KeybordS.openKeybord(shapeEt,activity);

                    }
                }, 200);
                if(slide!=null){
                    slide.showPopupWindow();
                }else {
                    selectComment();
                }
                slide.judgeKeybord(activity);
                break;
        }
    }

    private void init(View vie) {
        simpleTvCancle = vie.findViewById(R.id.simple_tv_cancle);
        simpleTvReport = vie.findViewById(R.id.simple_tv_report);
        simpleList = vie.findViewById(R.id.simple_list);
        //适配数据
        setAdapter();
    }
    //适配数据
    private void setAdapter() {
        Resources resources = activity.getResources();
        String[] array = resources.getStringArray(R.array.report);
        ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(activity,R.layout.list_item,array);
        simpleList.setAdapter(arrayAdapter);
        simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(map.get(index)==null){
                    TextView simpleTvItem=view.findViewById(R.id.item_tv_detail);
                    simpleTvItem.setTextColor(Color.parseColor("#f23a58"));
                    index=i;
                    map.put(i,simpleTvItem);
                }else {
                    TextView textView = map.get(index);
                    textView.setTextColor(Color.WHITE);
                    map.remove(index);
                    TextView simpleTvItem=view.findViewById(R.id.item_tv_detail);
                    simpleTvItem.setTextColor(Color.parseColor("#f23a58"));
                    index=i;
                    map.put(i,simpleTvItem);
                }


            }
        });
    }

    //关注动画
    private void showMenu() {
        if(!isAttentionStatus){
            startPropertyAnim();
            isAttentionStatus=!isAttentionStatus;

        }else {
            mVideoAddImg.setVisibility(View.GONE);
            mVideoTvAdd.setVisibility(View.GONE);
            reduce();
            isAttentionStatus=!isAttentionStatus;
        }
    }
    // 动画实际执行
    private void startPropertyAnim() {
        mVideoAddImg.setVisibility(View.GONE);
        mVideoTvAdd.setVisibility(View.GONE);
    }
    // 动画实际执行
    private void reduce() {

        LayoutTransition transition = new LayoutTransition();
        transition.setAnimator(LayoutTransition.CHANGE_APPEARING,
                null);
        mVideoAddImg.setVisibility(View.VISIBLE);
        mVideoTvAdd.setVisibility(View.VISIBLE);
        mVideoAttentionRela.setLayoutTransition(transition);
    }
    public boolean getAttentionStatus(){
        return isAttentionStatus;
    }//选择喜欢按钮
    private void selectLikeButton() {
        if (isLike){
            mBottombarImgLike.setImageResource(R.drawable.like_pressed);
            addLike();
            isLike=false;
        }else {
            mBottombarImgLike.setImageResource(R.drawable.like);
            reduceLike();
            isLike=true;
        }
    }
    //取消喜欢
    private void reduceLike() {
    }

    //提交用户喜好
    private void addLike() {
    }
    //设置底部的view弹出的动画
    private void setAnim() {
        if(isPopUp){
            Animation animation = AnimationUtils.loadAnimation(activity, R.anim.pop_exit_anim);
            bottomView.startAnimation(animation);
            bottomView.setVisibility(View.GONE);
            isPopUp=false;
        }else {
            Animation animation = AnimationUtils.loadAnimation(activity, R.anim.pop_enter_anim);
            bottomView.startAnimation(animation);
            bottomView.setVisibility(View.VISIBLE);
            isPopUp=true;

        }

    }
    //点击评论按钮
    private void selectComment() {
        if(slide==null){
            slide = new SlideBottomEdit(activity);
        }
        slide.setInputMethodReplace();
        slide.showPopupWindow();

        //获取输入框
        shapeEt = slide.getEditText();
        //设置点击监听事件
        shapeEt.setOnClickListener(this);
    }
    //点击转发按钮
    private void selectTransmit() {
        SlideBottomShare share=new SlideBottomShare(activity);
        share.showPopupWindow();
    }
    //点击打赏按钮
    private void selectReward() {
        SlideBottomReward reward=new SlideBottomReward(activity);
        reward.showPopupWindow();

    }


    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()){
            case MotionEvent.ACTION_DOWN:
                final float x = motionEvent.getX();
                final float y = motionEvent.getY();
                mInitialMotionX = x;
                mInitialMotionY = y;
                Log.d("zzz","downper");

                return true;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                final float upX = motionEvent.getX();
                final float upY= motionEvent.getY();
                final float dx = upX - mInitialMotionX;
                final float dy = upY - mInitialMotionY;
                Log.d("zzz","open");
                if(Math.abs(dx)>mTouchSlop){
                    if(Math.abs(dx)>Math.abs(dy)) {
                        if (dx > 0) {
                            Intent intent = new Intent(activity, WriteScreenActivity.class);
                            activity.startActivity(intent);
                            activity.overridePendingTransition(R.anim.trans_left_in, R.anim.trans_right_out);
                        } else {
                            Intent intent = new Intent(activity, PersonalActivity.class);
                            activity.startActivity(intent);
                            activity.overridePendingTransition(R.anim.tran_in, R.anim.tran_out);
                        }
                        return true;
                    }else {
                        return false;
                    }
                }


        }
        return true;
    }
}
